/**
 * 
 */
package com.cait.dutyfree.action.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.cait.dutyfree.dataaccess.InventoryServiceDAO;
import com.cait.dutyfree.pojo.Airline;
import com.cait.dutyfree.pojo.AirlineProduct;
import com.cait.dutyfree.pojo.Category;
import com.cait.dutyfree.pojo.Currency;
import com.cait.dutyfree.pojo.Product;
import com.cait.dutyfree.pojo.Stock;
import com.cait.dutyfree.pojo.SubCategory;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Rasika Kaluwalgoda
 * 
 */
public class InventoryAction extends ActionSupport implements
		ServletContextAware, SessionAware, ServletRequestAware {

	private static final long serialVersionUID = -2747451109877964085L;

	private ServletContext servletContext;
	private HttpServletRequest request;
	private Map<String, Object> session;

	private InventoryServiceDAO inventoryServiceDAO;

	private String code;
	private String name;
	private String desc;

	private String cat;
	private String catKey;
	private String subCat;
	private String subCatKey;

	private String prd;
	private String prdKey;

	private String prdImageUrl1;
	private String prdImageUrl2;
	private String prdImageUrl3;
	private String vendor;
	private int reOrderLevel;
	private int clt;
	private int qty;

	private List<String> selectedAirlines;

	private Integer[] airlineCurrencies;
	private Integer[] airlineUnitPrices;
	private Integer[] airlineDiscountRates;

	private Category category;
	private SubCategory subCategory;
	private Product product;

	private List<Category> categories = new ArrayList<Category>();
	private List<SubCategory> subCategories = new ArrayList<SubCategory>();
	private List<Product> products = new ArrayList<Product>();
	private List<Airline> airlines = new ArrayList<Airline>();
	private List<Currency> currencies = new ArrayList<Currency>();
	private List<Stock> stocks = new ArrayList<Stock>();
	private List<AirlineProduct> airlineDetails = new ArrayList<AirlineProduct>();

	private File img1;
	private File img2;
	private File img3;

	private String img1FileName;
	private String img2FileName;
	private String img3FileName;
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	public String loadCreateCategoryPage() {
		return SUCCESS;
	}

	public String loadCreateSubCategoryPage() {

		try {
			setCategories(getInventoryServiceDAO().getCategories());
		} catch (Exception e) {
			System.out.println(e);
			// TODO: handle exception
			return NONE;
		}
		return SUCCESS;
	}

	public String createCategory() {

		boolean isValid = true;

		try {

			Category category = new Category();
			category.setStatus(true);
			category.setDescription((getDesc().trim()));

			if (getCode().trim().equals("")) {
				isValid = false;
				addFieldError("code", "Code can not be empty");
			} else if (getInventoryServiceDAO().isAvailableCategoryCode(
					getCode().trim())) {
				isValid = false;
				addFieldError("code", "Code already exsist");
			} else {
				category.setCode(getCode().trim());
			}

			if (getName().trim().equals("") || getName().trim().length() < 2) {
				isValid = false;
				addFieldError("name",
						"Name can not be empty or less than 2 charactors");
			} else if (getInventoryServiceDAO().isAvailableCategoryName(
					getName().trim())) {
				isValid = false;
				addFieldError("code", "Name already exsist");
			} else {
				category.setName(getName().trim());
			}

			if (isValid) {
				getInventoryServiceDAO().createCategory(category);
				addActionMessage("Category created Successfully !!!");
				return SUCCESS;
			}
			return NONE;
		} catch (Exception e) {
			System.out.println(e);
		}
		return SUCCESS;
	}

	public String viewCategories() {

		try {
			setCategories(getInventoryServiceDAO().getCategories());
		} catch (Exception e) {
			// TODO: handle exception
			return NONE;
		}
		return SUCCESS;
	}

	public String viewCategory() {

		try {
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setCategory(getInventoryServiceDAO().getCategory(id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String loadEditCategoryPage() {

		try {
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setCategory(getInventoryServiceDAO().getCategory(id));
			getSession().put("catId", id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String editCategory() {

		boolean isValidForm = true;

		try {
			int id = Integer.parseInt(getSession().get("catId").toString());
			setCategory(getInventoryServiceDAO().getCategory(id));
			getSession().remove("catId");

			if (getName().trim().equals("") || getName().trim().length() < 2) {
				isValidForm = false;
				addFieldError("name",
						"Name can not be empty or less than 2 charactors");
			} else if (getInventoryServiceDAO().isAvailableCategoryNameTwice(
					getName().trim())) {
				isValidForm = false;
				addFieldError("name", "Name already exsist");
			} else {
				getCategory().setName(getName().trim());
			}

			getCategory().setDescription(getDesc().trim());

			if (isValidForm) {
				getInventoryServiceDAO().updateCategory(getCategory());
				addActionMessage("Category updated Successfully !!!");
				return SUCCESS;
			}

		} catch (Exception e) {
			// TODO: handle exception
			addActionMessage("Category updated failed !!!");
			return NONE;
		}
		addActionMessage("Category not updated !!!");
		return NONE;
	}

	public String activateCategory() {

		try {
			viewCategories();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setCategory(getInventoryServiceDAO().getCategory(id));
			getCategory().setStatus(true);
			getInventoryServiceDAO().updateCategory(getCategory());
			addActionMessage("Category activated successfully !!!");
			viewCategories();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Category not activated !!!");
			return NONE;
		}

	}

	public String deleteCategory() {

		try {
			viewCategories();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setCategory(getInventoryServiceDAO().getCategory(id));
			getCategory().setStatus(false);
			getInventoryServiceDAO().updateCategory(getCategory());
			addActionMessage("Category removed successfully !!!");
			viewCategories();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Category not removed !!!");
			return NONE;
		}

	}

	public String createSubCategory() {

		loadCreateSubCategoryPage();
		boolean isValid = true;

		try {

			SubCategory subCategory = new SubCategory();
			subCategory.setStatus(true);
			subCategory.setDescription(getDesc().trim());

			if (getCatKey().equals("")) {
				isValid = false;
				addFieldError("category", "Select a category");
			} else {
				subCategory.setCategory(getInventoryServiceDAO().getCategory(
						Integer.parseInt(getCatKey())));
			}

			if (getCode().trim().equals("")) {
				isValid = false;
				addFieldError("code", "Code can not be empty");
			} else if (getInventoryServiceDAO().isAvailableSubCategoryCode(
					getCode().trim())) {
				isValid = false;
				addFieldError("code", "Code already exsist");
			} else {
				subCategory.setCode(getCode().trim());
			}

			if (getName().trim().equals("") || getName().trim().length() < 2) {
				isValid = false;
				addFieldError("name",
						"Name can not be empty or less than 2 charactors");
			} else if (getInventoryServiceDAO().isAvailableSubCategoryName(
					getName().trim())) {
				isValid = false;
				addFieldError("code", "Name already exsist");
			} else {
				subCategory.setName(getName().trim());
			}

			if (isValid) {
				getInventoryServiceDAO().createSubCategory(subCategory);
				addActionMessage("Sub Category created Successfully !!!");
				return SUCCESS;
			}
			return NONE;

		} catch (Exception e) {
			// TODO: handle exception
			return NONE;
		}

	}

	public String viewSubCategories() {

		try {
			loadCreateSubCategoryPage();
			setSubCategories(getInventoryServiceDAO().getSubCategories());
		} catch (Exception e) {
			// TODO: handle exception
			return NONE;
		}
		return SUCCESS;

	}

	public String viewSubCategoriesByCategory() {

		try {
			setCategories(getInventoryServiceDAO().getCategories());
			setSubCategories(getInventoryServiceDAO()
					.getSubCategoriesByCategory(Integer.parseInt(getCatKey())));
		} catch (Exception e) {
			// TODO: handle exception
			return NONE;
		}
		return SUCCESS;

	}

	public String viewSubCategory() {

		try {
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setSubCategory(getInventoryServiceDAO().getSubCategory(id));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String loadEditSubCategoryPage() {

		try {
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setSubCategory(getInventoryServiceDAO().getSubCategory(id));
			getSession().put("subCatId", id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String editSubCategory() {

		loadCreateSubCategoryPage();
		boolean isValid = true;

		try {
			int id = Integer.parseInt(getSession().get("subCatId").toString());
			setSubCategory(getInventoryServiceDAO().getSubCategory(id));
			getSession().remove("subCatId");

			getSubCategory().setDescription(getDesc().trim());

			if (getName().trim().equals("") || getName().trim().length() < 2) {
				isValid = false;
				addFieldError("name",
						"Name can not be empty or less than 2 charactors");
			} else if (getInventoryServiceDAO()
					.isAvailableSubCategoryNameTwice(getName().trim())) {
				isValid = false;
				addFieldError("code", "Name already exsist");
			} else {
				getSubCategory().setName(getName().trim());
			}

			if (isValid) {
				getInventoryServiceDAO().updateSubCategory(getSubCategory());
				addActionMessage("Sub Category updated successfully !!!");
				return SUCCESS;
			}
			addActionMessage("Sub Category not updated !!!");
			return NONE;

		} catch (Exception e) {
			addActionMessage("Sub Category updated failed !!!");
			return NONE;
		}

	}

	public String activateSubCategory() {

		try {
			viewSubCategories();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setSubCategory(getInventoryServiceDAO().getSubCategory(id));
			getSubCategory().setStatus(true);
			getInventoryServiceDAO().updateSubCategory(getSubCategory());
			addActionMessage("Sub Category activated successfully !!!");
			viewSubCategories();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Sub Category not activated !!!");
			return NONE;
		}

	}

	public String deleteSubCategory() {

		try {
			viewSubCategories();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			setSubCategory(getInventoryServiceDAO().getSubCategory(id));
			getSubCategory().setStatus(false);
			getInventoryServiceDAO().updateSubCategory(getSubCategory());
			addActionMessage("Sub Category removed successfully !!!");
			viewSubCategories();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Sub Category not removed !!!");
			return NONE;
		}

	}

	public String showSubCategories() {

		try {
			setSubCategories(getInventoryServiceDAO()
					.getSubCategoriesByCategory(Integer.parseInt(getCat())));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String showSubCategories4Products() {
		try {
			setSubCategories(getInventoryServiceDAO()
					.getSubCategoriesByCategory(Integer.parseInt(getCat())));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String loadCreateProduct() {

		try {
			setCategories(getInventoryServiceDAO().getCategories());
			setAirlines(getInventoryServiceDAO().getAirlines());
			for (int i = 0; i < getAirlines().size(); i++) {
				Airline a = getAirlines().get(i);
				System.out.println(a.getName());
			}
			setCurrencies(getInventoryServiceDAO().getCurrencies());
			for (int i = 0; i < this.getCurrencies().size(); i++) {
				Currency c = getCurrencies().get(i);
				System.out.println("Currency:" + c.getCode());
			}
			if (getCat() == null || getCat().equals(0)) {

			} else {
				setSubCategories(getInventoryServiceDAO()
						.getSubCategoriesByCategory(Integer.parseInt(getCat())));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return NONE;
		}
		return SUCCESS;
	}

	public String nextInProduct() {
		loadCreateProduct();

		Product product = new Product();
		boolean isValidForm = true;
		try {

			if (getCat().equals("0")) {
				isValidForm = false;
				addFieldError("category", "Select a category");
			} else {
				product.setCategory(getInventoryServiceDAO().getCategory(
						Integer.parseInt(getCat())));
			}

			if (getSubCat() == null || getSubCat().equals("0")) {
				isValidForm = false;
				addFieldError("subCategory", "Select a sub-category");
			} else {
				product.setSubCategory(getInventoryServiceDAO().getSubCategory(
						Integer.parseInt(getSubCat())));
			}

			if (getCode().trim().equals("")) {
				isValidForm = false;
				addFieldError("code", "Product code can not be empty");
			} else if (getInventoryServiceDAO().isAvailableProductCode(
					getCode().trim())) {
				isValidForm = false;
				addFieldError("code", "Product code already exsist");
			} else {
				product.setCode(getCode().trim());

			}

			if (getName().trim().equals("")) {
				isValidForm = false;
				addFieldError("name", "Product name can not be empty");
			} else if (getInventoryServiceDAO().isAvailableProductName(
					getName().trim())) {
				isValidForm = false;
				addFieldError("name", "Product name already exsist");
			} else {
				product.setName(getName().trim());
			}

			product.setDescription(getDesc().trim());

			if (getVendor().trim().equals("")) {
				isValidForm = false;
				addFieldError("name", "Product vendor can not be empty");
			} else {
				product.setVendor(getVendor().trim());
			}

			if (getReOrderLevel() <= 0) {
				isValidForm = false;
				addFieldError("re-order",
						"Product re-oder level should be grater than zero");
			} else {
				product.setReOrderLevel(getReOrderLevel());
			}

			if (getClt() <= 0) {
				isValidForm = false;
				addFieldError("clt",
						"Product Critical Lower Threshold should be grater than zero");
			} else {
				product.setClt(getClt());
			}

			for (int s : getAirlineCurrencies()) {
				if (s == 0) {
					isValidForm = false;
					addFieldError("currency",
							"Please select currency for each selected airlines");
					break;
				}
			}

			for (int s : getAirlineUnitPrices()) {
				if (s == 0) {
					isValidForm = false;
					addFieldError("unitprice",
							"Please select unit price for each selected airlines");
					break;
				}
			}

			ArrayList<AirlineProduct> details = new ArrayList<AirlineProduct>();
			AirlineProduct detail;

			for (int i = 0; i < getSelectedAirlines().size(); i++) {

				detail = new AirlineProduct();
				detail.setAirline(getInventoryServiceDAO().getAirline(
						Integer.parseInt(getSelectedAirlines().get(i))));
				detail.setDiscountRate(getAirlineDiscountRates()[i]);
				detail.setUnitPrice(getAirlineUnitPrices()[i]);
				detail.setCurrency(getInventoryServiceDAO().getCurrency(
						getAirlineCurrencies()[i]));
				detail.setProduct(product);

				details.add(detail);

			}

			product.setAirlineProducts(details);

			if (isValidForm) {

				getSession().put("product", product);
				return SUCCESS;
			}
			return NONE;

		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			return NONE;
		}
	}

	public String createProduct() {

		boolean isValidForm = true;

		try {
			Product product = (Product) getSession().get("product");

			if (product == null) {
				isValidForm = false;
				addFieldError("img1", "Product creation is not completed");
				return NONE;
			}

			String filename = product.getCode() + "_img";
			String path = org.apache.struts2.ServletActionContext
					.getServletContext().getRealPath("/");
			path += "/uploadImages/";

			if (getImg1() == null && getImg2() == null && getImg3() == null) {
				isValidForm = false;
				addFieldError("img1", "Atleast 1 image should be attached");
			}

			if (getImg1() != null) {
				String ext = getImg1FileName().substring(
						(getImg1FileName().lastIndexOf(".") + 1),
						getImg1FileName().length());
				filename = filename + "1." + ext;
				File fileToCreate = new File(path, filename);
				FileUtils.copyFile(getImg1(), fileToCreate);
				product.setPrdImageUrl1(filename);
			}

			if (getImg2() != null) {
				String ext = getImg2FileName().substring(
						(getImg2FileName().lastIndexOf(".") + 1),
						getImg2FileName().length());
				filename = filename + "2." + ext;
				File fileToCreate = new File(path, filename);
				FileUtils.copyFile(getImg2(), fileToCreate);
				product.setPrdImageUrl2(filename);
			}

			if (getImg3() != null) {
				String ext = getImg3FileName().substring(
						(getImg3FileName().lastIndexOf(".") + 1),
						getImg3FileName().length());
				filename = filename + "3." + ext;
				File fileToCreate = new File(path, filename);
				FileUtils.copyFile(getImg3(), fileToCreate);
				product.setPrdImageUrl3(filename);
			}

			if (isValidForm) {
				getInventoryServiceDAO().createProduct(product);
				addActionMessage("Product created successfully !!!");
				getSession().put("prdCode", product.getCode());
				return "showProduct";
			}
		} catch (Exception e) {
			// TODO: handle exception
			return ERROR;
		}
		return NONE;

	}

	public String viewProductsPage() {

		try {
			setProducts(getInventoryServiceDAO().getProducts());
			setCategories(getInventoryServiceDAO().getCategories());
			if (getCat() == null || getCat().equals(0)) {

			} else {
				setSubCategories(getInventoryServiceDAO()
						.getSubCategoriesByCategory(Integer.parseInt(getCat())));
				if (getSubCat() != null && getSubCat().equals("0")) {
					setProducts(getInventoryServiceDAO().getProducts(
							getInventoryServiceDAO().getCategory(
									Integer.parseInt(getCat()))));
				}
			}

			if (getSubCat() != null && !getSubCat().equals("0"))
				setProducts(getInventoryServiceDAO().getProducts(
						getInventoryServiceDAO().getSubCategory(
								Integer.parseInt(getSubCat()))));

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return NONE;
		}
		return SUCCESS;
	}

	public String viewProduct() {

		try {
			int id = 0;
			if (getRequest().getParameter("id") != null) {
				id = Integer.parseInt(getRequest().getParameter("id"));
				setProduct(getInventoryServiceDAO().getProduct(id));
			} else {
				setProduct(getInventoryServiceDAO().getProduct(
						getRequest().getAttribute("prdCode").toString()));
				getRequest().removeAttribute("prdCode");
			}

			setAirlineDetails(getProduct().getAirlineProducts());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String successProduct() {

		try {
			setProduct(getInventoryServiceDAO().getProduct(
					getSession().get("prdCode").toString()));
			getSession().remove("prdCode");
			setAirlineDetails(getProduct().getAirlineProducts());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String loadStockAdjustmentPage() {

		try {
			setCategories(getInventoryServiceDAO().getCategories());
			if (getCat() == null || getCat().equals(0)) {

			} else {
				setSubCategories(getInventoryServiceDAO()
						.getSubCategoriesByCategory(Integer.parseInt(getCat())));
			}
		} catch (Exception e) {
			// TODO: handle exception
			return NONE;
		}
		return SUCCESS;
	}

	public String showSubCategories4StockAdjustment() {
		try {
			setSubCategories(getInventoryServiceDAO()
					.getSubCategoriesByCategory(Integer.parseInt(getCat())));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String showProducts() {

		try {
			if (getSubCat() != null)
				setProducts(getInventoryServiceDAO().getProducts(
						getInventoryServiceDAO().getSubCategory(
								Integer.parseInt(getSubCat()))));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String activateProduct() {

		try {
			viewProductsPage();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			Product product = getInventoryServiceDAO().getProduct(id);
			product.setStatus(true);
			getInventoryServiceDAO().updateProduct(product);
			addActionMessage("Product activated successfully !!!");
			viewProductsPage();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Product not activated !!!");
			return NONE;
		}

	}

	public String deleteProduct() {

		try {
			viewProductsPage();
			int id = Integer.parseInt(getRequest().getParameter("id"));
			Product product = getInventoryServiceDAO().getProduct(id);
			product.setStatus(false);
			getInventoryServiceDAO().updateProduct(product);
			addActionMessage("Product deactivated successfully !!!");
			viewProductsPage();
			return SUCCESS;
		} catch (Exception e) {
			addActionMessage("Product not deactivated !!!");
			return NONE;
		}

	}

	public String stockAdjustment() {

		loadStockAdjustmentPage();
		Stock stock = new Stock();
		try {
			if (getPrd() != null && !getPrd().equals("0") && getQty() > 0) {

				Product product = getInventoryServiceDAO().getProduct(
						Integer.parseInt(getPrd()));
				stock.setCategory(product.getCategory());
				stock.setSubCategory(product.getSubCategory());
				stock.setProduct(product);
				stock.setQuantity(getQty());

				getInventoryServiceDAO().updateStock(stock);
				addActionMessage("Stock updated successfully !!!");
				return SUCCESS;
			} else {
				addFieldError("category", "Stock not updated!!!");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String loadStockLedger() {

		try {
			setStocks(getInventoryServiceDAO().getStocks());
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return SUCCESS;
	}

	//======================================================= edit product
	private Integer id;
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String editProduct() {
		try {
			this.setCurrencies(this.getInventoryServiceDAO().getCurrencies());
			this.setProduct(getInventoryServiceDAO().getProduct(this.getId()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	private Integer[] airlineProductId;
	
	public Integer[] getAirlineProductId() {
		return airlineProductId;
	}

	public void setAirlineProductId(Integer[] airlineProductId) {
		this.airlineProductId = airlineProductId;
	}

	public String submitEditProduct(){
		try {
			this.product = this.getInventoryServiceDAO().getProduct(this.getId());
			this.product.setCode(this.getCode());
			this.product.setName(this.getName());
			this.product.setDescription(this.getDesc());
			this.product.setVendor(this.getVendor());
			this.product.setReOrderLevel(this.getReOrderLevel());
			this.product.setClt(this.getClt());
			List<AirlineProduct> airlineProducts = this.product.getAirlineProducts();
			for(int i = 0; i < airlineProductId.length; i++){
				System.out.println("airline Product id:" + airlineProductId[i]);
				String currencyId = this.getRequest().getParameter("currency_" + airlineProductId[i]);
				String unitPrice = this.getRequest().getParameter("unitPrice_" + airlineProductId[i]);
				String discountRate = this.getRequest().getParameter("discountRate_" + airlineProductId[i]);
				for(int j = 0; j < airlineProducts.size(); j++){
					if((airlineProducts.get(j)).getId().intValue() == airlineProductId[i].intValue()){
						AirlineProduct airPrd = airlineProducts.get(j);
						airPrd.setCurrency(this.getInventoryServiceDAO().getCurrency(Integer.valueOf(currencyId)));
						airPrd.setUnitPrice(Integer.valueOf(unitPrice));
						airPrd.setDiscountRate(Integer.valueOf(discountRate));
					}
				}
			}
			this.getInventoryServiceDAO().updateProduct(this.product);
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	//======================================================== end edit product

	public String productDetailsJSON() {
		Integer productId = Integer.valueOf(request.getParameter("prd"));
		System.out.println("productId:" + productId);
		try {
			this.setProduct(inventoryServiceDAO.getProduct(productId));
		} catch (Exception e) {

		}
		return SUCCESS;
	}

	public String productAttributes() {
		Integer productId = Integer.valueOf(request.getParameter("prd"));
		System.out.println("productId:" + productId);
		try {
			this.setProduct(inventoryServiceDAO.getProduct(productId));
		} catch (Exception e) {

		}
		return SUCCESS;
	}

	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}

	private ServletContext getServletContext() {
		return servletContext;
	}

	@SuppressWarnings("unchecked")
	public void setSession(@SuppressWarnings("rawtypes") Map session) {
		this.session = session;
	}

	private Map<String, Object> getSession() {
		return session;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	private HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @return the inventoryServiceDAO
	 */
	private InventoryServiceDAO getInventoryServiceDAO() {
		return inventoryServiceDAO;
	}

	/**
	 * @param inventoryServiceDAO
	 *            the inventoryServiceDAO to set
	 */
	public void setInventoryServiceDAO(InventoryServiceDAO inventoryServiceDAO) {
		this.inventoryServiceDAO = inventoryServiceDAO;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the desc
	 */
	public String getDesc() {
		return desc;
	}

	/**
	 * @param desc
	 *            the desc to set
	 */
	public void setDesc(String desc) {
		this.desc = desc;
	}

	/**
	 * @return the cat
	 */
	public String getCat() {
		return cat;
	}

	/**
	 * @param cat
	 *            the cat to set
	 */
	public void setCat(String cat) {
		this.cat = cat;
	}

	/**
	 * @return the catKey
	 */
	public String getCatKey() {
		return catKey;
	}

	/**
	 * @param catKey
	 *            the catKey to set
	 */
	public void setCatKey(String catKey) {
		this.catKey = catKey;
	}

	/**
	 * @return the categories
	 */
	public List<Category> getCategories() {
		return categories;
	}

	/**
	 * @param categories
	 *            the categories to set
	 */
	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	/**
	 * @return the subCategories
	 */
	public List<SubCategory> getSubCategories() {
		return subCategories;
	}

	/**
	 * @param subCategories
	 *            the subCategories to set
	 */
	public void setSubCategories(List<SubCategory> subCategories) {
		this.subCategories = subCategories;
	}

	/**
	 * @return the airlines
	 */
	public List<Airline> getAirlines() {
		return airlines;
	}

	/**
	 * @param airlines
	 *            the airlines to set
	 */
	public void setAirlines(List<Airline> airlines) {
		this.airlines = airlines;
	}

	/**
	 * @return the category
	 */
	public Category getCategory() {
		return category;
	}

	/**
	 * @param category
	 *            the category to set
	 */
	public void setCategory(Category category) {
		this.category = category;
	}

	/**
	 * @return the subCategory
	 */
	public SubCategory getSubCategory() {
		return subCategory;
	}

	/**
	 * @param subCategory
	 *            the subCategory to set
	 */
	public void setSubCategory(SubCategory subCategory) {
		this.subCategory = subCategory;
	}

	/**
	 * @return the subCat
	 */
	public String getSubCat() {
		return subCat;
	}

	/**
	 * @param subCat
	 *            the subCat to set
	 */
	public void setSubCat(String subCat) {
		this.subCat = subCat;
	}

	/**
	 * @return the subCatKey
	 */
	public String getSubCatKey() {
		return subCatKey;
	}

	/**
	 * @param subCatKey
	 *            the subCatKey to set
	 */
	public void setSubCatKey(String subCatKey) {
		this.subCatKey = subCatKey;
	}

	/**
	 * @return the prdImageUrl1
	 */
	public String getPrdImageUrl1() {
		return prdImageUrl1;
	}

	/**
	 * @param prdImageUrl1
	 *            the prdImageUrl1 to set
	 */
	public void setPrdImageUrl1(String prdImageUrl1) {
		this.prdImageUrl1 = prdImageUrl1;
	}

	/**
	 * @return the prdImageUrl2
	 */
	public String getPrdImageUrl2() {
		return prdImageUrl2;
	}

	/**
	 * @param prdImageUrl2
	 *            the prdImageUrl2 to set
	 */
	public void setPrdImageUrl2(String prdImageUrl2) {
		this.prdImageUrl2 = prdImageUrl2;
	}

	/**
	 * @return the prdImageUrl3
	 */
	public String getPrdImageUrl3() {
		return prdImageUrl3;
	}

	/**
	 * @param prdImageUrl3
	 *            the prdImageUrl3 to set
	 */
	public void setPrdImageUrl3(String prdImageUrl3) {
		this.prdImageUrl3 = prdImageUrl3;
	}

	/**
	 * @return the vendor
	 */
	public String getVendor() {
		return vendor;
	}

	/**
	 * @param vendor
	 *            the vendor to set
	 */
	public void setVendor(String vendor) {
		this.vendor = vendor;
	}

	/**
	 * @return the reOrderLevel
	 */
	public int getReOrderLevel() {
		return reOrderLevel;
	}

	/**
	 * @param reOrderLevel
	 *            the reOrderLevel to set
	 */
	public void setReOrderLevel(int reOrderLevel) {
		this.reOrderLevel = reOrderLevel;
	}

	/**
	 * @return the clt
	 */
	public int getClt() {
		return clt;
	}

	/**
	 * @param clt
	 *            the clt to set
	 */
	public void setClt(int clt) {
		this.clt = clt;
	}

	/**
	 * @return the selectedAirlines
	 */
	public List<String> getSelectedAirlines() {
		return selectedAirlines;
	}

	/**
	 * @param selectedAirlines
	 *            the selectedAirlines to set
	 */
	public void setSelectedAirlines(List<String> selectedAirlines) {
		this.selectedAirlines = selectedAirlines;
	}

	/**
	 * @return the airlineCurrencies
	 */
	public Integer[] getAirlineCurrencies() {
		return airlineCurrencies;
	}

	/**
	 * @param airlineCurrencies
	 *            the airlineCurrencies to set
	 */
	public void setAirlineCurrencies(Integer[] airlineCurrencies) {
		this.airlineCurrencies = airlineCurrencies;
	}

	/**
	 * @return the airlineUnitPrices
	 */
	public Integer[] getAirlineUnitPrices() {
		return airlineUnitPrices;
	}

	/**
	 * @param airlineUnitPrices
	 *            the airlineUnitPrices to set
	 */
	public void setAirlineUnitPrices(Integer[] airlineUnitPrices) {
		this.airlineUnitPrices = airlineUnitPrices;
	}

	/**
	 * @return the airlineDiscountRates
	 */
	public Integer[] getAirlineDiscountRates() {
		return airlineDiscountRates;
	}

	/**
	 * @param airlineDiscountRates
	 *            the airlineDiscountRates to set
	 */
	public void setAirlineDiscountRates(Integer[] airlineDiscountRates) {
		this.airlineDiscountRates = airlineDiscountRates;
	}

	/**
	 * @return the currencies
	 */
	public List<Currency> getCurrencies() {
		return currencies;
	}

	/**
	 * @param currencies
	 *            the currencies to set
	 */
	public void setCurrencies(List<Currency> currencies) {
		this.currencies = currencies;
	}

	/**
	 * @return the products
	 */
	public List<Product> getProducts() {
		return products;
	}

	/**
	 * @param products
	 *            the products to set
	 */
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	/**
	 * @return the prd
	 */
	public String getPrd() {
		return prd;
	}

	/**
	 * @param prd
	 *            the prd to set
	 */
	public void setPrd(String prd) {
		this.prd = prd;
	}

	/**
	 * @return the prdKey
	 */
	public String getPrdKey() {
		return prdKey;
	}

	/**
	 * @param prdKey
	 *            the prdKey to set
	 */
	public void setPrdKey(String prdKey) {
		this.prdKey = prdKey;
	}

	/**
	 * @return the qty
	 */
	public int getQty() {
		return qty;
	}

	/**
	 * @param qty
	 *            the qty to set
	 */
	public void setQty(int qty) {
		this.qty = qty;
	}

	/**
	 * @return the stocks
	 */
	public List<Stock> getStocks() {
		return stocks;
	}

	/**
	 * @param stocks
	 *            the stocks to set
	 */
	public void setStocks(List<Stock> stocks) {
		this.stocks = stocks;
	}

	/**
	 * @return the img1
	 */
	public File getImg1() {
		return img1;
	}

	/**
	 * @param img1
	 *            the img1 to set
	 */
	public void setImg1(File img1) {
		this.img1 = img1;
	}

	/**
	 * @return the img2
	 */
	public File getImg2() {
		return img2;
	}

	/**
	 * @param img2
	 *            the img2 to set
	 */
	public void setImg2(File img2) {
		this.img2 = img2;
	}

	/**
	 * @return the img3
	 */
	public File getImg3() {
		return img3;
	}

	/**
	 * @param img3
	 *            the img3 to set
	 */
	public void setImg3(File img3) {
		this.img3 = img3;
	}

	/**
	 * @return the img1FileName
	 */
	public String getImg1FileName() {
		return img1FileName;
	}

	/**
	 * @param img1FileName
	 *            the img1FileName to set
	 */
	public void setImg1FileName(String img1FileName) {
		this.img1FileName = img1FileName;
	}

	/**
	 * @return the img2FileName
	 */
	public String getImg2FileName() {
		return img2FileName;
	}

	/**
	 * @param img2FileName
	 *            the img2FileName to set
	 */
	public void setImg2FileName(String img2FileName) {
		this.img2FileName = img2FileName;
	}

	/**
	 * @return the img3FileName
	 */
	public String getImg3FileName() {
		return img3FileName;
	}

	/**
	 * @param img3FileName
	 *            the img3FileName to set
	 */
	public void setImg3FileName(String img3FileName) {
		this.img3FileName = img3FileName;
	}

	/**
	 * @return the product
	 */
	public Product getProduct() {
		return product;
	}

	/**
	 * @param product
	 *            the product to set
	 */
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * @return the airlineDetails
	 */
	public List<AirlineProduct> getAirlineDetails() {
		return airlineDetails;
	}

	/**
	 * @param airlineDetails
	 *            the airlineDetails to set
	 */
	public void setAirlineDetails(List<AirlineProduct> airlineDetails) {
		this.airlineDetails = airlineDetails;
	}

}
